//
//  GuidePageController.swift
//  d11
//
//  Created by Wai Yin Chan on 28/9/2017.
//  Copyright © 2017 Tech_001. All rights reserved.
//

import Foundation
import UIKit

class GuidePageController: UIViewController{
    var pageViewController: GuidePageViewController?
    var launchViewController: LaunchViewController?
    var loginViewController: LoginViewController?
    var hideButton = true
    var hideSkip = true
    var hideGuideSkip = true
    var keepGuideSkipFunctionality = false
    
    var skipTimer: Timer?
    var skipRemain = 4
    var isLaunch = false
    var backgroundImage: UIImageView! = UIImageView()
    @IBOutlet weak var enterButton: UIButton!
    @IBOutlet weak var skipButton: UIButton!
    @IBOutlet weak var guidePageSkipButton: UIButton!
    var image: UIImage?
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
         let defaults = UserDefaults.standard
          defaults.set(false, forKey: "mt_isLog")
        view.addSubview(backgroundImage)
        view.sendSubviewToBack(backgroundImage)
        backgroundImage.translatesAutoresizingMaskIntoConstraints = false
        backgroundImage.anchors.topAnchor.constraintEqualToAnchor(anchor: view.anchors.topAnchor).isActive = true
        backgroundImage.anchors.bottomAnchor.constraintEqualToAnchor(anchor: view.anchors.bottomAnchor).isActive = true
        backgroundImage.anchors.leadingAnchor.constraintEqualToAnchor(anchor: view.anchors.leadingAnchor).isActive = true
        backgroundImage.anchors.trailingAnchor.constraintEqualToAnchor(anchor: view.anchors.trailingAnchor).isActive = true
        backgroundImage.contentMode = .scaleToFill

        /**** ****/
        enterButton.layer.isHidden = hideButton
        skipButton.layer.isHidden = hideSkip
        guidePageSkipButton.layer.isHidden = hideGuideSkip

        if !hideSkip {
            skipButton.backgroundColor = UIColor.init(red: 0.57, green: 0.37, blue: 0.1, alpha: 0.8)
            UIHelper.round(skipButton, with: [.bottomLeft,.topLeft,.bottomRight, .topRight], size: CGSize(width: skipButton.frame.width, height: skipButton.frame.height))

            //Timer
            self.skipRemain = 4
            self.skipTimer = Timer()
            self.skipTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.timerTick), userInfo: nil, repeats: true)
            RunLoop.main.add(self.skipTimer!, forMode: RunLoop.Mode.common)
        }

        if hideGuideSkip && keepGuideSkipFunctionality {
            guidePageSkipButton.layer.isHidden = false
            guidePageSkipButton.setImage(nil, for: .normal)
        }

        if image != nil {
            backgroundImage.image = image!
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
         let layerGradient = CAGradientLayer()
        layerGradient.colors = [
            UIColor.init(red: 1, green: 0.78, blue: 0.45, alpha: 0.9).cgColor,
            UIColor.init(red: 0.99, green: 0.57, blue: 0.33, alpha: 0.9).cgColor
        ]
        layerGradient.frame = CGRect(x: 0, y: 0, width: self.enterButton.width, height: self.enterButton.height)
        self.enterButton?.layer.addSublayer(layerGradient)
        self.enterButton?.layer.cornerRadius = self.enterButton.height/2;
        self.enterButton?.layer.masksToBounds = true
        self.enterButton?.setTitle("立即体验", for: .normal)
    }
    
    func updateImage(_ image: UIImage){
       backgroundImage.image = image
    }
    
    @IBAction func enterClick(_ sender: Any) {
//        cocoa-- 注释
//        ToolHelper.statisticsPage(pageName: "登录页-成功登录-跳过")
//        print("登录页-成功登录-跳过")
//        self.skipTimer?.invalidate()
//        if(self.pageViewController != nil){
//             self.pageViewController?.launchViewController?.switchLoginView()
//        }else{
//            self.launchViewController?.enterTabBarViewController()
//        }
//
//
//        self.dismiss(animated: false, completion: nil)
//
//        if let loginViewController = self.loginViewController {
//            loginViewController.continueMainViewController(self)
//        }
    }
    
    @objc func timerTick(){
        skipRemain -= 1
         print("登录页-\(skipRemain)")
        if skipRemain <= 0 {
            enterClick(self)
        }
       
        skipButton.setTitle("\(skipRemain) | 跳过", for: .normal)
      
    }
}
